package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import logic.NewConfirmLogic;
import model.Shain;

import util.Authorizer;
import exception.BusinessException;
import exception.NotLoginedException;
import exception.SystemException;

/**
 * Servlet implementation class NewConfirmServlet
 */
@WebServlet("/NewConfirmServlet")
public class NewConfirmServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewConfirmServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//リクエストとレスポンスの文字コードをUTF-8に設定
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//ログインを確認
		try{
			Authorizer a = new Authorizer(request);
			a.isLogined();
		}
		//ログインしていない場合、ログイン画面に遷移させる
		catch (NotLoginedException nle){
			this.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}
		
		//社員情報モデルクラスの作成
		Shain shain = new Shain();

		//社員情報モデルクラスにパラメータを格納する
		//TODO パラメータの取得、リクエストスコープへの格納を機能分割する
		shain.shain_no = request.getParameter("shain_no");
		shain.password = request.getParameter("password");
		shain.password_conf = request.getParameter("password_conf");
		shain.first_name = request.getParameter("first_name");
		shain.last_name = request.getParameter("last_name");
		shain.sex = request.getParameter("sex");
		shain.enter_date = request.getParameter("enter_date");
		shain.section_code = request.getParameter("section_code");
		shain.e_mail = request.getParameter("e_mail");
		
		//リクエストスコープにパラメータを格納する
		request.setAttribute("shain_no", shain.shain_no);
		request.setAttribute("password", shain.password);
		request.setAttribute("password_conf", shain.password_conf);
		request.setAttribute("first_name", shain.first_name);
		request.setAttribute("last_name", shain.last_name);
		request.setAttribute("sex", shain.sex);
		request.setAttribute("enter_date", shain.enter_date);
		request.setAttribute("section_code", shain.section_code);
		request.setAttribute("e_mail", shain.e_mail);
		
		//登録確認ロジックを実行する
		try{
			NewConfirmLogic logic = new NewConfirmLogic();
			logic.executeNewConfirm(shain);
		}
		//システム例外が発生した場合、システムエラー画面に遷移
		catch (SystemException se){
			this.getServletContext().getRequestDispatcher("/system_error.jsp").forward(request, response);
			return;
		}
		//業務例外が発生した場合、登録画面に遷移
		catch (BusinessException be){
			request.setAttribute("errorMessages", be.getErrorMessages());
			this.getServletContext().getRequestDispatcher("/new.jsp").forward(request, response);
			return;
		}
		
		//認証済みの場合、登録確認画面に遷移
		this.getServletContext().getRequestDispatcher("/new_confirm.jsp").forward(request, response);
	}
}
